package com.box.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.box.entity.DbAdmin;
import com.box.entity.DbFiles;
import com.box.entity.FilesSearch;
import com.box.service.AdminService;
import com.box.service.FilesService;
import com.box.utils.LayuiResult;
import com.box.utils.SessionUtils;

@Controller
@RequestMapping("/files")
public class FilesController {

	@Autowired
	private FilesService fileService;
	
	@Autowired
	private AdminService adminService;
	/**
	 * 业务逻辑
	 * 
	 * 
	 */
	
	@RequestMapping(value="/geFilesList",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult selFileList(Integer page,Integer limit,FilesSearch search){
		
		
		return fileService.selFilesList(page, limit, search);	
	}
	
	@RequestMapping(value="/delFilesById/{id}",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult delFileByOne(@PathVariable("id") String id){
		
		Integer n = fileService.delFileById(Long.parseLong(id));
		if(n>0){
			return new LayuiResult(0, "删除成功");
			
		}else{
			
			return new LayuiResult(500,"删除失败");
			
		}

	}
	
	@RequestMapping(value="/delFilesAll/{FilesStr}",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult delFileByMany(@PathVariable("FilesStr") String FilesStr){
		
		Integer n = fileService.delFileByFileStr(FilesStr);
		if(n>0){
			return new LayuiResult(0, "批量删除成功");
			
		}else{
			
			return new LayuiResult(500,"批量删除失败");
			
		}

	}
	
	
	@RequestMapping(value="/updfiles",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult updateFileByObject(DbFiles files){
		
		Integer n = fileService.updateFilesByObject(files);
		if(n>0){
			return new LayuiResult(0, "修改成功");
			
		}else{
			
			return new LayuiResult(500,"修改失败");
			
		}

	}
	
	
	@RequestMapping(value="/addfiles",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult addFileByObject(DbFiles files){
		
		Integer n = fileService.addFilesByObject(files);
		if(n>0){
			return new LayuiResult(0, "添加成功");
			
		}else{
			
			return new LayuiResult(500,"添加失败");
			
		}

	}
	/**
	 *页面跳转
	 * 
	 * 
	 */
	@RequestMapping(value="/page/fileList",method={RequestMethod.GET,RequestMethod.POST})
	public String showFilesPage(Model model){
		DbAdmin admins = adminService.getDbAdminItems();
		model.addAttribute("admin", admins);
		
		return "file/filesList";	
	}
	
	@RequestMapping(value="/page/editFiles/{id}",method={RequestMethod.GET,RequestMethod.POST})
	public String showEditFilesPage(@PathVariable("id") String id,Model model){
		DbAdmin admins = adminService.getDbAdminItems();
		model.addAttribute("admin", admins);
		DbFiles files = fileService.selFilesById(Long.parseLong(id));
		model.addAttribute("files",files);
		return "file/editFiles";	
	}
	
	@RequestMapping(value="/page/addFiles",method={RequestMethod.GET,RequestMethod.POST})
	public String showAddFilesPage(Model model){
		DbAdmin admins = adminService.getDbAdminItems();
		model.addAttribute("admin", admins);
	
		return "file/addFiles";	
	}
	
	
}
